<template>
  <div>
    <div>【大区数据信息】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})

// 获取 dom 实例
const target = ref(null)


let myChart = null
onMounted(() => {
  myChart = echarts.init(target.value)
  render()
})
const render = () => {
  const option = {
    grid: {
      top: 0,
      left: 0,
      rigtht: 0,
      bottom: 0,
      containLabel: true
    },
    xAxis: {
      show: false
    },
    yAxis: {
      data: props.data.regions.map((item) => item.name),
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      }
    },
    series: {
      type: 'bar',
      max: 100,
      data: props.data.regions.map((item) => item.value),
      barWidth: 15,
      barCategoryGap: 50,
      itemStyle: {
        barBorderRadius: 20
      },
      showBackground: true,
      backgroundStyle: {
        borderRadius: 20,
        color: 'rgba(180, 180, 180, 0.2)'
      },
      label: {
        show: true,
        position: 'right',
        color: 'white'
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    }
  }
  myChart.setOption(option)
}
watch(
  () => props.data,
  (newValue) => {
    render()
  }
)
</script>

<style lang="scss" scoped></style>
